struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
};

class Solution
{
public:
    int dfs(int num, TreeNode *root)
    {
        if (root == nullptr)
        {
            return 0;
        }
        int sum = num * 10 + root->val;

        if (root->left == nullptr && root->right == nullptr)
        {
            return sum;
        }
        else
        {
            return dfs(sum, root->left) + dfs(sum, root->right);
        }
    }
    int sumNumbers(TreeNode *root)
    {
        return dfs(0, root);
    }
};